#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

void work()
{
    int cnt = 5;
    while(cnt--)
    {
        printf("working.., cnt:%d\n",cnt);
        sleep(1);
    }
}

int main()
{   
    pid_t id = fork();
    if(id == 0)
    {
        printf("child\n");
        int cnt = 5;
        while(cnt--)
        {
            work();
            sleep(1);
        }
        exit(0);
    }
    else{
        while(1)
        {
        int status = 0;
        pid_t rid = waitpid(id, &status, WNOHANG);
        if(rid > 0)
        {
            printf("wait success,exit code: %d, exit singal: %d, rid: %d\n", (status>>8)&0xFF, status&0x7F, rid);
            exit(0);
        }
        else if(rid == 0)
        {
            printf("wait again\n");
        }
        else{
            printf("wait failed\n");
            exit(1);
            
        }
    
        }
    }
    return 0;
}
